/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.idearth.TouchTalk.Directory;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Edit extends Activity {

	private EditText nameText;
    private EditText phoneText;
    private EditText ipText;
    private TextView lastText;
    private Long rowId;
    private DirectoryDbAdapter dirDbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dirDbHelper = new DirectoryDbAdapter(this);
        dirDbHelper.open();
        setContentView(R.layout.directory_edit);
        
       
        nameText = (EditText) findViewById(R.id.name);
        phoneText = (EditText) findViewById(R.id.phone);
        ipText = (EditText) findViewById(R.id.ip);
        lastText = (TextView) findViewById(R.id.last_date);
        Button confirmButton = (Button) findViewById(R.id.confirm);
       
        rowId = savedInstanceState != null ? savedInstanceState.getLong(DirectoryDbAdapter.KEY_ROWID) 
               							: null;
        
        if (rowId == null) {
			Bundle extras = getIntent().getExtras();            
			rowId = extras != null ? extras.getLong(DirectoryDbAdapter.KEY_ROWID) 
									: null;
		}

		populateFields();
		
        confirmButton.setOnClickListener(new View.OnClickListener() {

        	public void onClick(View view) {
        	    setResult(RESULT_OK);
        	    finish();
        	}
          
        });
    }
    
    private void populateFields() {
        if (rowId != null) {
            Cursor note = dirDbHelper.fetchContact(rowId);
            startManagingCursor(note);
            nameText.setText(note.getString(
    	            note.getColumnIndexOrThrow(DirectoryDbAdapter.KEY_NAME)));
            phoneText.setText(note.getString(
                    note.getColumnIndexOrThrow(DirectoryDbAdapter.KEY_PHONE)));
            ipText.setText(note.getString(
                    note.getColumnIndexOrThrow(DirectoryDbAdapter.KEY_IP)));
            lastText.setText(note.getString(
                    note.getColumnIndexOrThrow(DirectoryDbAdapter.KEY_LAST_DATE)));

        }
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putLong(DirectoryDbAdapter.KEY_ROWID, rowId);
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        saveState();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        populateFields();
    }
    
    private void saveState() {
        String name = nameText.getText().toString();
        String phone = phoneText.getText().toString();
        String ip = ipText.getText().toString();
        String last_date = lastText.getText().toString();
        if (rowId == null) {
        	if(name.length()==0){
        		finish();
        	}
        	else{
	            long id = dirDbHelper.createContact(name, phone, ip, last_date);
	            if (id > 0) {
	                rowId = id;
	            }
        	}
        } else {
            dirDbHelper.updateContact(rowId, name, phone, ip, last_date);
        }
    }
    
}
